/**
 * libnlog -- User friendly logging system with network support.
 *
 * Copyright (C) 2011
 * 	Samuel Nussbaum <samuel (dot) nussbaum (at) e-robot.ch>
 *
 * This library is free software; you can redistribute it and/or modify it under the terms of the
 * GNU Lesser General Public License as published by the Free Software Foundation; either version 3
 * of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this
 * library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301 USA
 */
#ifndef __NLOG_NETWORKLOGGER_H__
#define __NLOG_NETWORKLOGGER_H__

#include "Types.hpp"
#include "Logger.hpp"

namespace boost {
	namespace asio {
		class io_service;
		template <typename Protocol> class datagram_socket_service;
		template <typename Protocol, typename DatagramSocketService> class basic_datagram_socket;
		namespace ip {
			class udp;
			class address;
			template <typename InternetProtocol> class basic_endpoint;
		}
	}
}

namespace nlog {

class NetworkLogger: public Logger
{
	public:
		typedef ::boost::asio::io_service Service;
		typedef ::boost::asio::ip::address Address;
		typedef ::boost::asio::ip::basic_endpoint< ::boost::asio::ip::udp> Endpoint;
		typedef ::boost::asio::basic_datagram_socket< ::boost::asio::ip::udp,
			::boost::asio::datagram_socket_service< ::boost::asio::ip::udp> > Socket;
		virtual ~NetworkLogger() { /* Nothing to do... */ }
		virtual void write(const Message &_message) const;
		NetworkLogger& sendTo(const String &_address, short _port);
		static void receiveFrom(const String &_address, const String &_multicast, short _port);
		static void stop();
		static NetworkLogger &instance(const String &);

	protected:
		NetworkLogger(const Logger &_base) : Logger(_base), m_endpoint(NULL), m_socket(NULL) { /* Nothing to do... */ }
		static void run(String _address, String _multicast, short _port);
		static const SizeT m_kHeaderSize = 8;
		static Thread *m_thread;
		static Service *m_service;
		Endpoint *m_endpoint;
		Socket *m_socket;

};

}

#endif /* __NLOG_NETWORKLOGGER_H__ */
